package com.lorenzogao.pley.common.rx.subscriber

import android.content.Context
import com.lorenzogao.pley.common.util.ProgressDialogHandler
import io.reactivex.disposables.Disposable

/**
 * 作者：Lorenzo Gao
 * Date: 2018/1/2
 * Time: 13:50
 * 邮箱：2508719070@qq.com
 * Description:
 */
abstract class ProgressDialogSubcriber<T> constructor(context: Context) : ErrorHandlerSubscriber<T>(context), ProgressDialogHandler.OnProgressCancelListener {

    private var mProgressDialogHandler: ProgressDialogHandler? = null

    private lateinit var mDisposable: Disposable;


    init {
        mProgressDialogHandler = ProgressDialogHandler(mContext!!, true, this)
    }

    protected fun isShowProgressDialog(): Boolean {
        return true
    }

    override fun onCancelProgress() {
        mDisposable.dispose()
    }

   override  fun onSubscribe(d: Disposable) {
        this.mDisposable = d
        if (isShowProgressDialog()) {
            this.mProgressDialogHandler!!.showProgressDialog()
        }
    }


    override fun onComplete() {
        if (isShowProgressDialog()) {
            this.mProgressDialogHandler!!.dismissProgressDialog()
        }
    }


    override fun onError(e: Throwable?) {
        super.onError(e)
        if (isShowProgressDialog()) {
            this.mProgressDialogHandler!!.dismissProgressDialog()
        }
    }


}